﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;

namespace YY.UI
{

    public partial class EditOptionFrm : BaseFrm
    {

        public int Id { get; set; }
        public int ParentId { get; set; }
        public bool EditMode { get; set; }
        /// <summary>
        /// 通用选项类型
        /// </summary>
        public YY.Common.EnumCommon.OptionType OType { get; set; }
        /// <summary>
        /// 临时变量
        /// </summary>
        private TB_Options Option { get; set; }

        /// <summary>
        /// 编码规则
        /// </summary>
        public CodeRuleFrm.CodeRule codeRule { get; set; }

        public EditOptionFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new BaseDataClient();
        }

        private void EditOptionFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Option = new TB_Options();
            this.codeRule = new CodeRuleFrm.CodeRule();//取默认编码规则


            if (this.EditMode)
            {
                this.textBox1.Enabled = false;
                Option = this.BaseDataProxy.GetOption(this.Id);
                if (Option != null)
                {
                    this.Text = "编辑" + Option.type;
                    this.textBox1.Text = Option.code;
                    this.textBox2.Text = Option.name;
                }
            }
            else
            {
                if (this.codeRule.autoCode)
                {
                    this.textBox1.Text = this.BaseDataProxy.GetSerialNo((int)YY.Common.EnumCommon.CodeRuleType.Other, this.codeRule.header, this.codeRule.len, !string.IsNullOrEmpty(this.codeRule.header), this.codeRule.autoFill, false);
                    this.textBox1.Enabled = false;
                }
                this.Text = "新增" + this.OType.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string code = this.textBox1.Text.Trim();
            string name = this.textBox2.Text.Trim();
            if (string.IsNullOrEmpty(code))
            {
                MessageBox.Show("请输入代码！");
                this.textBox1.Focus();
                return;
            }

            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请输入名称！");
                this.textBox2.Focus();
                return;
            }

            Option.code = code;
            Option.name = name;

            Common.OperateType opType = Common.OperateType.Add;
            if (this.EditMode)
            {
                this.BaseDataProxy.UpdateOption(Option);
                opType = Common.OperateType.Update;
            }
            else
            {
                Option.parentId = this.ParentId;
                Option.type = this.OType.ToString();
                int id = BaseDataProxy.AddOption(Option);
                Option.id = id;
            }
            //YY.Common.EventCommon.NodeEventArgs arg = new YY.Common.EventCommon.NodeEventArgs(isNewNode, Option.code, Option.name, Option.id.ToString());
            CommonEventArg<TB_Options> arg = new CommonEventArg<TB_Options>(Option, opType);            
            YY.Common.EventCommon.OnOptionUdTVEvnt(sender, arg);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
